
exports.api = {
	useremail:function(val){ //邮箱验证
		var obj={};
		obj.ret = true;
		obj.message = '';
		$.ajax(
			{
				url:"/ajax/tyCheckemail",
				type:"post",
				async:false,
				data:"email="+val,
				success:function(res){
					if(res.code != 200){
						obj.ret = false;
					}
					obj.message = res.message;
				},
				error:function(){
					obj.ret = false;
				}
			}
		);
		return obj;
	},
	username:function(val){ //用户名验证
		var obj={};
		obj.ret = true;
		obj.message = '';

		$.ajax(
			{
				url:"/ajax/tyCheckusername",
				type:"post",
				async:false,
				data:"username="+val,
				success:function(res){
					if(res.code != 200){
						obj.ret = false;
					}
					obj.message = res.message;
				},
				error:function(){
					obj.ret = false;
				}
			}
		);
		return obj;
	},
	hasmobile:function(val){
		var obj={};
		obj.ret = true;
		obj.message = '';
		$.ajax(
			{
				url:"/ajax/tyCheckmobile",
				type:"post",
				async:false,
				data:"mobile="+val,
				success:function(res){
					if(res.code != 200){
						obj.ret = false;
					}
					obj.message = res.message;
				},
				error:function(){
					obj.ret = false;
				}
			}
		);
		return obj;
	},
	captcha:function(val){ //图形验证码
		var ret = true;
		$.ajax(
			{
				url:"/ajax/checkVcode",
				type:"post",
				async:false,
				data:{
					type : "jiekuanapply",
					code : val
				},
				dataType:'json',
				success:function(res){
					if(res.code != 200){
						ret = false;
					}
				},
				error:function(){
					ret = false;
				}
			}
		);
		return ret;
	},
    httpregname:function(val){
		var ret = true;
		$.ajax(
			{
				url:"/activity/verifyUname?uname="+val,
				async:false,
				success:function(res){
					if(res !=1){
						ret =false;
					}
				},
				error:function(){
					ret = false;
				}
			}
		);
		return ret;
	},
	regname:function(val){
		var ret = true;
		$.ajax(
			{
				url:"/user/verifyUname?uname="+val,
				async:false,
				success:function(res){
					if(res !=1){
						ret =false;
					}
				},
				error:function(){
					ret = false;
				}
			}
		);
		return ret;
	},
	regInvests:function(val){
		var ret = true;
		$.ajax(
			{
				url:"/fund/verifyCompanyRecord",
				type : 'post',
				data : 'company='+val,
				async:false,
				success:function(res){
					if(res !=1){
						ret =false;
					}
				},
				error:function(){
					ret = false;
				}
			}
		);
		return ret;
	},
	regtimeoutInvests:function(val){
		var ret = true;
		$.ajax(
			{
				url:"/fund/verifyCompanyRun",
				type : 'post',
				data : 'company='+val,
				async:false,
				success:function(res){
					if(res !=1){
						ret =false;
					}
				},
				error:function(){
					ret = false;
				}
			}
		);
		return ret;
	},
	regemail:function(val){
		var ret = true;
		$.ajax({
			url : "/user/verifyUemail?uemail="+val,
			async:false,
			success:function(res){
				if(res !=1){
					ret =false;
				}
			},
			error:function(){
				ret = false;
			}
		});
		return ret;
	},
	equal:function(val,nameId){
		if(val !==$(nameId).val())
			return false;
		return true;
	},
	notempty:function(val){
		if($.trim(val) ==='')
			return false;
		return true;
	},
	len:function(val,minlen,maxlen){
		if(val.length<minlen || val.length>maxlen)
			return false;
		return true;
	},
	cha:function(val,reg){
		if(new RegExp(reg).test(val))
			return true;
		return false;
	},
	number:function(val){
		if(/^\d+$/.test(val))
			return true;
		return false;
	},
	user_name:function(val){

	},
	firstplace_cn_or_num:function(val) {
		var reg = /^[\u4e00-\u9fa5|a-z|A-z]/i;
		if(reg.test(val))
			return true;
		return false;
	},
	floatNumber : function(val,floatLen){
		if(floatLen){
			var reg = new RegExp('\^\\d+(\\.\\d{1,' + floatLen + '})\?\$');

		}else{
			var reg = /^\d+((\.{1}\d+)|\d?)$/;
		}
		
		if(reg.test(val))
			return true;
		return false;
	},
	cn : function(val){
		var reg = /^[\u4e00-\u9fa5]+$/;
		if(reg.test(val))
			return true;
		return false;
	},
	filter : function(val){
		var reg = /^[0-9a-zA-Z_]{1,}$/;
		if(reg.test(val))
			return true;
		return false;
	},
	reg_cn_letter_number : function(val){
		var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
		if(reg.test(val))
			return true;
		return false;
	},
	reg_cn_len : function(val,minlen,maxlen){
		var len = val.replace(/[^\x00-\xFF]/g,'***').length;
		if(len<minlen || len>maxlen)
			return false;
		return true;
	},
	reg_cn_len2 : function(val,minlen,maxlen){
		var len = val.replace(/[^\x00-\xFF]/g,'**').length;
		if(len<minlen || len>maxlen)
			return false;
		return true;
	},
	pwd : function(val){ // pwd : 密码校验规则，至少包含 数字/字母/符号 中的两种。
		var reg = /((?=.*\d)(?=.*\D)|(?=.*[a-zA-Z])(?=.*[^a-zA-Z]))^.{1,}$/;
		if(reg.test(val))
			return true;
		return false;
	},
	equal: function(val, equalIdSelector){			// 判断表单内容是否相等的规则
		var equalVal = $("#" + equalIdSelector).val();
		if(val == equalVal)
			return true;
		return false;
	},
	email:function(val){
		if(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(val))
			return true;
		return false;
	},
	mobile:function(val){
		if(!val) return false;
	  	val = val.replace(/\s+/g,'');

		if(/^(13\d|18\d|15\d|17\d|14\d|16\d|19\d)\d{8}$/.test(val))
			return true;
		return false;
	},
    notmobile:function(val){
		if(/1\d{10}/.test(val))
			return false;
		return true;
	},
	ID_card : function(val){
		val = $.trim(val);
		// return (/(^\d{15}$)|(^\d{16}$)|(^\d{18}$)|(^\d{19}$)|(^\d{17}(\d|X|x)$)/.test(val))
		return (/^[1-9][0-7]\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X)?$/.test(val))
	},
	numberRegion: function(val,min,max){
		if(/\d+/.test(val)){
			val = val - 0;
			min = min - 0;
			max = max - 0;
			if(val > max || val < min || isNaN(val)) return false;

			return true;

		}else{
			return false;
		}
	},
	USCredit_Code: function(Code){ //Unified_Social_Credit_Code 统一社会信用码
		Code = $.trim(Code);
		var patrn = /^[0-9A-Z]+$/;
		//18位校验及大写校验
		if ((Code.length != 18) || (patrn.test(Code) == false)) {
			// console.info("不是有效的统一社会信用编码！");
			return false;
		}
		else {
			var Ancode;//统一社会信用代码的每一个值
			var Ancodevalue;//统一社会信用代码每一个值的权重
			var total = 0;
			var weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];//加权因子
			var str = '0123456789ABCDEFGHJKLMNPQRTUWXY';
			//不用I、O、S、V、Z
			for (var i = 0; i < Code.length - 1; i++) {
				Ancode = Code.substring(i, i + 1);
				Ancodevalue = str.indexOf(Ancode);
				total = total + Ancodevalue * weightedfactors[i];
				//权重与加权因子相乘之和
			}
			var logiccheckcode = 31 - total % 31;
			if (logiccheckcode == 31) {
				logiccheckcode = 0;
			}
			var Str = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y";
			var Array_Str = Str.split(',');
			logiccheckcode = Array_Str[logiccheckcode];
	
	
			var checkcode = Code.substring(17, 18);
			if (logiccheckcode != checkcode) {
				// console.info("不是有效的统一社会信用编码！");
				return false;
			}

			return true;
		}
	},
	IDorUSC_Code: function(val){   //社会信用代码/身份证号 满足其中一个
		if(exports.api["ID_card"](val) || exports.api["USCredit_Code"](val)){
			return true;
		}
		return false;
	}

};

//验证元素
exports.valid = function(item){
	var $item = $(item),
		validResult = {};
	
	if($item[0] && $item[0].tagName.toUpperCase()==='FORM'){
		var items = $item.find('[data-valid]'),
			i = 0,
			l = items.length,
			ret = true;

		for(;i<l;i++){
			ret = exports.valid(items.eq(i));
			if(ret && ret.error){
				validResult = ret;
				break;
			}
		}
		return validResult;

	}else{
		var valid = $item.attr('data-valid'),
			configs = valid.split('|'),
			validFn,
			validParams,
			validConfig,
			i = 0,
			l = configs.length;

			
		for(;i<l;i++){
			validConfig = configs[i].split(':');
			validFn = validConfig[0];
			validParams = validConfig.slice(1);
			validParams.unshift($item.val());
			if(exports.api[validFn] && $.isFunction(exports.api[validFn])){
				var ret = exports.api[validFn].apply($item,validParams);
				// console.log(ret, validParams, exports.api[validFn])
				if(ret == false){
					validResult = {
						error:1,
						element:$item,
						valid:validFn
					};
					break;
				}else if(typeof(ret) == 'object'){
					if(ret.ret == false){
						validResult = {
							error:1,
							element:$item,
							valid:validFn,
							message:ret.message
						};
						break;
					}
				}

			}
		}
	}
	return validResult;
}